﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsView;
using WindowsFormsModel.BusinessObjects;
using WindowsFormsPresenter;
using WindowsFormsPresenter.Restaurant;

namespace WindowsFormApplication
{
    public partial class frmLogin : Form, ILoginView, IRestaurantsView
    {
        private LoginPresenter _loginPresenter;
        private RestaurantsPresenter _restaurantsPresenter;

        public IList<RestaurantModel> Restaurants
        {
            set { var restaurants = value; AddRestaurantToCombobox(restaurants); }
        }

        public string UserName
        {
            get { return txtUserName.Text.ToString(); }
        }

        public string Password
        {
            get { return txtPassword.Text.ToString(); }
        }

        public string RestaurantId
        {
            get { return cboRestaurantId.Value.ToString(); }
        }

        private void AddRestaurantToCombobox(IList<RestaurantModel> restaurants)
        {
            cboRestaurantId.DataSource = restaurants;
            cboRestaurantId.DisplayMember = "RestaurantName";
            cboRestaurantId.ValueMember = "RestaurantID";

            cboRestaurantId.Rows[0].Selected = true;
        }

        public frmLogin()
        {
            InitializeComponent();

            _restaurantsPresenter = new RestaurantsPresenter(this);
            _loginPresenter = new LoginPresenter(this);
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                _loginPresenter.Login();

                frmMain frm = new frmMain();
                frm.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Đăng nhập thất bại");
            }
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            _restaurantsPresenter.Display();
        }
    }
}
